#include "head.h"
#include <my_header.h>

void showdir(int netfd,char_stack_t* stack){
    char buf[2048];
    char* content=stack->buf;
    int count=stack->count;
    int* arr=stack->start;
    int buf_pointer=0;
    int content_pointer=0;
    for(int i=0;i<count;i++){
       int num=arr[i];
       buf[buf_pointer++]='/';
       for(int j=0;j<num;j++){
           buf[buf_pointer++]=content[content_pointer++];
       }
    }
    buf[buf_pointer]='\0';
    write(netfd,buf,strlen(buf));
}
int add_dir(char* words,char_stack_t* stack){
    char* content=stack->buf;
    int* arr=stack->start;
    arr[stack->count]=strlen(content);
    stack->count++;
    strcat(content,words);
    //"\0"的问题(strcat函数会自动补)
    return 0;
}
int back_dir(char_stack_t* stack){
    if(stack->count<=1){
        return -1;
    }
    char* content=stack->buf;
    int* arr=stack->start;
    int temp=stack->count-1;
    int index=arr[temp];
    content[index]='\0';
    stack->count--;
    return 0;
}
int back_to_root(char_stack_t* stack){
    if(stack->count==1) return 0;
    char* content=stack->buf;
    int* arr=stack->start;
    int temp=arr[1];
    content[temp]='\0';
    stack->count=1;
    return 0;
}
int exec_cd(int netfd,char_stack_t* stack,char* aim){
    char* pointer=aim;
    char buf[1024];
    int flag;
    int count;
    if(*pointer=='/'||*pointer=='~'){
        back_to_root(stack);
        if(*pointer=='~') pointer++;
    }
    while(*pointer!='\0'){
        if(*pointer=='/') pointer++;
        count=0;
        buf[count++]='/';
        flag=0;
        while(*pointer!='/'&&*pointer!='\0'){
            if(*pointer=='.'&&flag!=3){
                flag++;
            }
            else{
                flag=3;
            }
            buf[count++]=*pointer++;
        }
        if(flag==2) back_dir(stack);
        if(flag==3){
            
        }
        bzero(buf,sizeof(buf));
    }
    return 0;
}
